// 初始化代码块功能
document.addEventListener('DOMContentLoaded', function() {
    // 创建复制按钮
    const createCopyBtn = () => {
        const btn = document.createElement('button');
        btn.className = 'copy-btn';
        btn.innerHTML = '📋';
        btn.title = '复制代码';
        return btn;
    };

    // 添加行号
    const addLineNumbers = (pre) => {
        const lines = pre.textContent.split('\n').length - 1;
        const lineNumbers = document.createElement('div');
        lineNumbers.className = 'line-numbers';
        lineNumbers.innerHTML = Array(lines).fill('<span></span>').join('');
        pre.insertBefore(lineNumbers, pre.firstChild);
    };

    // 为所有pre元素添加功能
    document.querySelectorAll('pre').forEach(pre => {
        const wrapper = document.createElement('div');
        wrapper.className = 'code-block';
        pre.parentNode.replaceChild(wrapper, pre);
        wrapper.appendChild(pre);
        
        addLineNumbers(pre);
        const copyBtn = createCopyBtn();
        wrapper.appendChild(copyBtn);

        // 触发Prism高亮
        Prism.highlightElement(pre.querySelector('code'));

        // 复制功能
        copyBtn.addEventListener('click', () => {
            const code = pre.querySelector('code').textContent;
            navigator.clipboard.writeText(code).then(() => {
                const tip = document.createElement('div');
                tip.className = 'copy-tip';
                tip.textContent = '已复制！';
                wrapper.appendChild(tip);
                setTimeout(() => tip.remove(), 1500);
            });
        });
    });
});

// 添加Prism高亮
Prism.highlightAll();